<template>
  <van-button
    v-if="is_followed"
    class="follow-btn"
    round
    size="small"
    :loading="isFollowed"
    @click="doFollow"
  >已关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="isFollowed"
    @click="doFollow"
  >关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user.js'
export default {
  // 定义属性
  data() {
    return {
      isFollowed:false
    }
  },
  model: {
    prop: 'is_followed',
    event: 'updateFollow'
  },
  props: {
    is_followed: {
      type: Boolean,
      required:true
    },
    aut_id: {
      type:Number,
      required:true
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {  
    async doFollow() {
      try {
        this.isFollowed = true
        if (this.is_followed) {
          // 取消关注
          await deleteFollow(this.aut_id)
        } else {
          await addFollow(this.aut_id)
        }
        // 成功提示
        this.is_followed = !this.is_followed
        // 更新父组件
        this.$emit('updateFollow', this.is_followed)
        this.$toast('操作成功')
      } catch (error) {
        console.log(error.response)
        if (error.response && error.response.status === 400) {
          this.$toast('自己不能关注自己')
        }
      }
      this.isFollowed = false
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {}
}
</script>

<style lang='less'>
</style>